//
//  SendProgramViewController.m
//  QinrenManagers
//
//  Created by easaa on 15/8/7.
//  Copyright (c) 2015年 easaa. All rights reserved.
//
//
//
//     本页面应该用tableView写，用现有方法太耗内存
//
//
//
//
#import "SendProgramViewController.h"

#import "ChatController.h"
#import "ProgramListModel.h"

@interface SendProgramViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView *baseTableView;
    
    NSInteger indexPathRow;
}
@property (nonatomic, strong) NSMutableArray * dataSource;
/**发送链接的参数，保存选中的方案*/
@property (nonatomic, strong) NSString * st_id;

@end

@implementation SendProgramViewController

- (instancetype)init {
    self = [super init];
    if (self) {
        
        _dataSource = [NSMutableArray array];
        indexPathRow = -1;
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self requestForProgramListData];
    [self creatTableView];

}

- (void)requestForProgramListData
{
    
    EAHttpAPIClient *manager = [EAHttpAPIClient manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager GetWithParameters:nil method:@"get.solution.type" success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *resposeString ;
        if ([responseObject isKindOfClass:[NSData class]]) {
            resposeString = [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
        }else
            resposeString = responseObject;
        
        NSString *jsonString = [DataParser parseString:resposeString];
        id dictionary = [JSONKit jsonToArrayOrDictionary:jsonString];
        NSLog(@"方案列表的dictonary :%@",[dictionary description]);
        
        
        if ([[dictionary objectForKey:@"verification"]intValue] == 1)
        {
            for (NSDictionary *dataDic in dictionary[@"data"]) {
                
                ProgramListModel *programListModel = [[ProgramListModel alloc]init];
                [programListModel setValuesForKeysWithDictionary:dataDic];
                [_dataSource addObject:programListModel];
                
            }
            [baseTableView reloadData];
        }
        else
        {
            
            NSString *error = [dictionary objectForKey:@"error"];
            [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeClear];
            [SVProgressHUD showErrorWithStatus:error];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeClear];
        [SVProgressHUD showErrorWithStatus:[error localizedDescription]];
        
        
    }];
    
}

- (void)creatTableView {

    baseTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H) style:UITableViewStylePlain];
    baseTableView.delegate = self;
    baseTableView.dataSource = self;
    baseTableView.tableFooterView = [[UIView alloc]init];
    [self.view addSubview:baseTableView];
}


#pragma mark----------------tableViewDelegate  tableviewDataSource---------------------

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [_dataSource count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString * CellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    for (UIView *subView in cell.contentView.subviews) {
        [subView removeFromSuperview];
    }
    
    ProgramListModel *programListModel = _dataSource[indexPath.row];
    
//    UIButton * selectButton = [[UIButton alloc]initWithFrame:CGRectMake(10, 10, 30, 30)];
    UILabel  * titleLabel   = [[UILabel  alloc]initWithFrame:CGRectMake(60, 10, SCREEN_W-80, 40)];
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 30, 30)];
    if (programListModel.isSelect) {
        imageView.image = [UIImage imageNamed:@"s"];
    }else{
        imageView.image = [UIImage imageNamed:@"k"];
    }
    [cell.contentView addSubview:imageView];
//    [selectButton setBackgroundImage:[UIImage imageNamed:@"k"] forState:UIControlStateNormal];
//    [selectButton setBackgroundImage:[UIImage imageNamed:@"s"] forState:UIControlStateSelected];
    
//    [cell.contentView addSubview:selectButton];
    [cell.contentView addSubview:titleLabel];
//    [selectButton addTarget:self action:@selector(saveSelect:) forControlEvents:UIControlEventTouchUpInside];
    
//    selectButton.selected = programListModel.isSelect;
    
    titleLabel.text = programListModel.st_name;
    

    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {

    UIView *footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_W, 60)];
    
    
    UIButton * sendButton = [[UIButton alloc]initWithFrame:CGRectMake(40, 5, SCREEN_W-80, 50)];
    
    [sendButton setTitle:@"确认发送" forState:UIControlStateNormal];
    [sendButton setBackgroundImage:[UIImage imageNamed:@"queren"] forState:UIControlStateNormal];
    sendButton.layer.cornerRadius = 5;
    sendButton.clipsToBounds = YES;
    [sendButton addTarget:self action:@selector(onClikedButton) forControlEvents:UIControlEventTouchUpInside];
    
    [footView addSubview:sendButton];
    
    return footView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 60;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ProgramListModel *programListModel = _dataSource[indexPath.row];
    
//    if (indexPathRow == -1) {
//        
//        programListModel.isSelect = YES;
//        indexPathRow = indexPath.row;
//    }
//    else if(indexPathRow == indexPath.row){
//        programListModel.isSelect = !programListModel.isSelect;
//        if (!programListModel.isSelect) {
//            indexPathRow = -1;
//        }else{
//            indexPathRow = indexPath.row;
//        }
//        
//    }
//    else {
//        programListModel.isSelect = YES;
//        ProgramListModel *ppreListModel = _dataSource[indexPathRow];
//        ppreListModel.isSelect = NO;
//        indexPathRow = indexPath.row;
//    }
    
    programListModel.isSelect = !programListModel.isSelect;

    [baseTableView reloadData];
}

- (void)onClikedButton
{
    for ( ProgramListModel *programListModel in _dataSource) {
        if (programListModel.isSelect == YES) {
            _st_id = programListModel.st_id;
            NSString *sendURL = [NSString stringWithFormat:@"%@/app/solution.aspx?sid=%@&st_id=%@",baseUrla,self.solution_id,_st_id];
            
            [[NSNotificationCenter defaultCenter]postNotificationName:SendUrl object:sendURL];
        }
    }

    //_sendPara = [senderArr componentsJoinedByString:@","];
    //NSLog(@"%@",_sendPara);
    
   // http://192.168.0.107:810/app/solution.aspx?sid=34d2ce89-e360-44bc-a0a1-b6c76edfcb06&type=4
//    NSString *sendURL = [NSString stringWithFormat:@"%@/app/solution.aspx?sid=%@&st_id=%@",baseUrla,self.solution_id,_st_id];
//
//    [[NSNotificationCenter defaultCenter]postNotificationName:SendUrl object:sendURL];
    
    if ([self.navType integerValue] == 100) {
        [self.navigationController popToViewController:self.navigationController.viewControllers[self.navigationController.viewControllers.count-3] animated:YES];
    }
    else if ([self.navType integerValue] == 101) {
        [self.navigationController popToViewController:self.navigationController.viewControllers[self.navigationController.viewControllers.count-2] animated:YES];
    }
    
}


@end
